package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.YddAdvimg;
import com.itheima.youdianda.domain.YddAdvpos;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.dto.YddAdvimgDto;
import com.itheima.youdianda.mapper.YddAdvimgMapper;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import com.itheima.youdianda.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName YddAdvimgServiceImpl
 * @Description
 * @Author Young
 * @Date 2023/2/13 1:34
 * @Version 1.0
 */
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper,YddAdvimg> implements YddAdvimgService {

    @Autowired
    private YddAdvposService yddAdvposService;
    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Override
    public void savexinxinxi(YddAdvimg yddAdvimg) {
        yddAdvimgService.save(yddAdvimg);
    }

    @Override
    public int count(Integer id) {
        LambdaQueryWrapper<YddAdvimg> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos,id);
        int count = yddAdvimgService.count(dishLambdaQueryWrapper);
        return count;
    }
    @GetMapping("/rowInfo")
    public R<YddAdvimg> page(@RequestHeader("x-token") String token, int id) {
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            YddAdvimg byId = yddAdvimgService.getById(id);
            return R.success(byId);
        }
        return R.error("查询失败");
    }

    public PageDto<YddAdvimg> page( int page, int limit, String advimgdesc, String advimgpos) {
        //分页构造器
        Page<YddAdvimg> pageInfo = new Page<>(page, limit);
        Page<YddAdvimg> yddAdvimgDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(advimgdesc != "", YddAdvimg::getAdvimgdesc, advimgdesc);
        queryWrapper.like(advimgpos != "", YddAdvimg::getAdvimgpos, advimgpos);
        //添加排序条件，根据id进行排序
        queryWrapper.orderByAsc(YddAdvimg::getId);
        //分页查询
        yddAdvimgService.page(pageInfo, queryWrapper);
        BeanUtils.copyProperties(pageInfo, yddAdvimgDtoPage, "records");
        List<YddAdvimg> records = pageInfo.getRecords();
        List<YddAdvimg> list = records.stream().map((item) -> {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(item, yddAdvimgDto);
            String advimgpos1 = item.getAdvimgpos();
            //根据id查询名字
            YddAdvpos byId = yddAdvposService.getById(advimgpos1);
            if (byId != null) {
                String advposname = byId.getAdvposname();
                yddAdvimgDto.setAdvposname(advposname);
            }
            return yddAdvimgDto;
        }).collect(Collectors.toList());
        yddAdvimgDtoPage.setRecords(list);
        PageDto<YddAdvimg> pageDto = new PageDto<>();
        BeanUtils.copyProperties(yddAdvimgDtoPage, pageDto);
        pageDto.setValue(yddAdvimgDtoPage);
        return pageDto;
    }

}
